package com.ruoyi.product.domain;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;

/**
 * 商品SKU对象 product_sku
 *
 * @author liuhuizhong
 * @date 2025-07-11
 */
@Data
public class ProductSku extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * SKU ID
     */
    private Long id;

    /**
     * 所属SPU ID
     */
    @Excel(name = "所属SPU ID")
    private Long spuId;

    /**
     * SKU编码
     */
    @Excel(name = "SKU编码")
    private String code;

    /**
     * 商品分类ID
     */
    @Excel(name = "商品分类ID")
    private Long categoryId;
    /**
     * 模板ID
     */
    @Excel(name = "模板ID")
    private Long templateId;
    /**
     * 规格属性JSON
     */
    @Excel(name = "规格属性JSON")
    private String specData;
    /**
     * sku属性值列表
     */
    private List<ProductSkuSpecValue> specValues;

    /**
     * 销售价
     */
    @Excel(name = "销售价")
    private BigDecimal price;

    /**
     * 原价
     */
    @Excel(name = "原价")
    private BigDecimal originalPrice;

    /**
     * 成本价
     */
    @Excel(name = "成本价")
    private BigDecimal costPrice;

    /**
     * 库存数量
     */
    @Excel(name = "库存数量")
    private Long stock;

    /**
     * 库存预警值
     */
    @Excel(name = "库存预警值")
    private Long lowStock;

    /**
     * 重量(g) 用于物流
     */
    @Excel(name = "重量(g) 用于物流")
    private BigDecimal weight;

    /**
     * 体积(cm³) 用于物流
     */
    @Excel(name = "体积(cm³) 用于物流")
    private BigDecimal volume;

    /**
     * 主图
     */
    @Excel(name = "主图")
    private List<ProductImage> productImages;

    private String image;

    /**
     * 状态：0-禁用，1-启用
     */
    @Excel(name = "状态：0-禁用，1-启用")
    private Integer status;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date createdAt;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date updatedAt;

    /** spec組名 */
    private String specNameGroup;

    /** spec組值 */
    private String specValueGroup;

    /** 创建人 */
    private String createBy;

    /** 更新人 */
    private String updateBy;

    /** sku名称 */
    private String skuName;

    private String description;

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("spuId", getSpuId())
                .append("code", getCode())
                .append("categoryId", getCategoryId())
                .append("specData", getSpecData())
                .append("price", getPrice())
                .append("originalPrice", getOriginalPrice())
                .append("costPrice", getCostPrice())
                .append("stock", getStock())
                .append("lowStock", getLowStock())
                .append("weight", getWeight())
                .append("volume", getVolume())
                .append("image", getProductImages())
                .append("status", getStatus())
                .append("createdAt", getCreatedAt())
                .append("updatedAt", getUpdatedAt())
                .append("specNameGroup", getSpecNameGroup())
                .append("specValueGroup", getSpecValueGroup())
                .append("createBy", getCreateBy())
                .append("updateBy", getUpdateBy())
                .append("skuName", getSkuName())
                .toString();
    }



    // 基本的 getter/setter 方法
    @JsonIgnore
    public String getSpecNameGroup() {
        return specNameGroup;
    }

    public void setSpecNameGroup(String specNameGroup) {
        this.specNameGroup = specNameGroup;
    }

    @JsonIgnore
    public String getSpecValueGroup() {
        return specValueGroup;
    }

    public void setSpecValueGroup(String specValueGroup) {
        this.specValueGroup = specValueGroup;
    }

    // 用于 JSON 序列化的特殊方法
    @JsonProperty("specNameGroup")
    public List<String> getSpecNameGroupAsList() {
        if (specNameGroup != null && !specNameGroup.isEmpty() && !specNameGroup.equals("[]")) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.readValue(specNameGroup, new TypeReference<List<String>>(){});
            } catch (Exception e) {
                return new ArrayList<>();
            }
        }
        return new ArrayList<>();
    }

    @JsonProperty("specValueGroup")
    public List<String> getSpecValueGroupAsList() {
        if (specValueGroup != null && !specValueGroup.isEmpty() && !specValueGroup.equals("[]")) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.readValue(specValueGroup, new TypeReference<List<String>>(){});
            } catch (Exception e) {
                return new ArrayList<>();
            }
        }
        return new ArrayList<>();
    }

    // 用于接收前端传入的列表并转换为JSON字符串
    @JsonProperty("specNameGroupList")
    public void setSpecNameGroupList(List<String> specNameGroupList) {
        if (specNameGroupList != null && !specNameGroupList.isEmpty()) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                this.specNameGroup = objectMapper.writeValueAsString(specNameGroupList);
            } catch (Exception e) {
                this.specNameGroup = "[]";
            }
        } else {
            this.specNameGroup = "[]";
        }
    }

    @JsonProperty("specValueGroupList")
    public void setSpecValueGroupList(List<String> specValueGroupList) {
        if (specValueGroupList != null && !specValueGroupList.isEmpty()) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                this.specValueGroup = objectMapper.writeValueAsString(specValueGroupList);
            } catch (Exception e) {
                this.specValueGroup = "[]";
            }
        } else {
            this.specValueGroup = "[]";
        }
    }
}
